package com.moon.config;

import com.xxl.job.core.executor.impl.XxlJobSpringExecutor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Optional;

@Configuration
public class XxlJobConfiguration {

    @Bean
    public XxlJobSpringExecutor xxlJobExecutor(XxlJobProperties properties) {
        XxlJobSpringExecutor executor = new XxlJobSpringExecutor();

        XxlJobProperties.Admin adminProperties = properties.getAdmin();
        executor.setAdminAddresses(adminProperties.getAddresses());

        XxlJobProperties.Executor executorProperties = properties.getExecutor();
        executor.setAppname(executorProperties.getAppName());
        Optional.ofNullable(properties.getAccessToken()).ifPresent(executor::setAccessToken);
        Optional.ofNullable(executorProperties.getIp()).ifPresent(executor::setIp);
        Optional.ofNullable(executorProperties.getPort()).ifPresent(executor::setPort);
        Optional.ofNullable(executorProperties.getLogPath()).ifPresent(executor::setLogPath);
        Optional.ofNullable(executorProperties.getLogRetentionDays()).ifPresent(executor::setLogRetentionDays);
        return executor;
    }

}
